package com.demo.gateway.mapper.provider;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.demo.gateway.pojo.GatewayRouteDO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: demo
 * @date: 2020年6月22日
 * @Document : 路由映射提供类
 */
@Slf4j
public class RouteMapperProvider {

	public String addRouteProvider(GatewayRouteDO route) {
		String filters = JSONObject.toJSONString(route.getFilters());
		String predicates = JSONObject.toJSONString(route.getPredicates());
		log.info("-------filters==>{}", filters);

		String appendInsert = ProviderUtil.sqlValues(route.getId(), route.getUri(),
				ProviderUtil.sqlReverseEl(filters), predicates, route.getOrder());
		StringBuilder sb = new StringBuilder();
		sb.append("insert into gateway_routes(id,uri,filters,predicates,orders,createdate)  ").append(" values (")
				.append(appendInsert).append(" ,sysdate())");

		return sb.toString();
	}

	public String updateRouteProvider(GatewayRouteDO route) {

		String filters = JSONObject.toJSONString(route.getFilters());
		String predicates = JSONObject.toJSONString(route.getPredicates());
		log.info("-------filters==>{}", filters);
		Map<String, Object> updateMap = new HashMap<String, Object>(10);
		updateMap.put("uri", route.getUri());
		updateMap.put("filters", ProviderUtil.sqlReverseEl(filters));
		updateMap.put("predicates", predicates);
		updateMap.put("orders", route.getOrder());
		StringBuilder sb = new StringBuilder();
		sb.append("update gateway_routes set ").append(ProviderUtil.sqlUpdates(updateMap)).append(" where id=")
				.append(ProviderUtil.sqlString(route.getId()));
		return sb.toString();

	}
}
